'use strict';

const conn = require('../../libs/DB/Conn');
const operationRecord = require("../sys/operationRecordAction");

module.exports = async function (params) {
    try {
        let usedCostPlanList = await conn(`SELECT cost_plan_id AS costPlanId,count(*) AS count  FROM device_file  WHERE cost_plan_id IN (?) AND delete_sign = 1`,[params.costPlanIds])
        let usedCostPlanIds = [];
        usedCostPlanList.forEach(function (data){
            if(data.count > 0){
                usedCostPlanIds.push(data.costPlanId);
            }
        })
        if(usedCostPlanIds.length > 0){ throw {message:"存在正在使用的计费方案,无法删除"} }
        await conn(`UPDATE cost_plan_file SET delete_sign = 2 WHERE cost_plan_id IN (?)`,[params.costPlanIds])
        await operationRecord(params);
        return {success: true};
    }catch (e){
        return {success:false,message:e.message}
    }
};